package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.BaseVo;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RoleResourceMapper roleResourceMapper;
    @Override
    public PageResponse<RoleVo> findRolePage(RoleDto roleDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Role> roles = roleMapper.selectRolePage(roleDto);
        PageResponse<RoleVo> pageResponse = PageResponse.of(roles, RoleVo.class);
        return pageResponse;
    }

    @Override
    public Boolean createRole(RoleDto roleDto) {
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.insert(role);

        return true;
    }

    @Override
    public Set<String> findCheckedResources(Long roleId) {
        Set<String> resources = roleResourceMapper.findCheckedResources(roleId);
        log.info("resources返回的数据:{}",resources);
        return resources;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(List<String> roleIds) {

        roleMapper.deleteRole(roleIds);
        roleResourceMapper.deleteRoleResource(roleIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRole(RoleDto roleDto) {
        // 更新角色名字
        Role role = BeanUtil.toBean(roleDto, Role.class);
        roleMapper.updateRole(role);
        //根据有没有传编号判断是否是角色资源
        if (ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())){
            return true;
        }
        //更新角色资源 先删后加
        //删除原有角色资源中间信息
        Long id = role.getId();
        List<String> list = Arrays.asList(id.toString());
        roleResourceMapper.deleteRoleResource(list);
        //添加角色资源中间信息
        List<RoleResource> roleResourceList = Lists.newArrayList();
        for (String resourceNo : roleDto.getCheckedResourceNos()) {
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(id);
            roleResource.setResourceNo(resourceNo);
            roleResource.setDataState(SuperConstant.DATA_STATE_0);
            roleResourceList.add(roleResource);
        }
        //如果集合是空的，就结束请求
        if (ObjectUtil.isEmpty(roleResourceList)){
            return true;
        }
        //批量保存角色资源的关系数据
        roleResourceMapper.insertBatch(roleResourceList);

        return true;
    }

    @Override
    public List<RoleVo> initRole() {
       List<Role> roles = roleMapper.initRoleAll();
        //将List集合转成List<RoleVo>集合
        List<RoleVo> roleVos = BeanUtil.copyToList(roles, RoleVo.class);
        return roleVos;
    }
}
